[contents] [prev] [top] (6 out of 6)

Pipes

Pipes are used to iterate over a source collection, range, or other sequenceable class. Each element is fed through the pipe to a function, or a collection class or instance. Pipes are often a useful shorthand for many nested for loops.

from | to
The pipe operator is an infix shorthand for calling the pipe generic function, which you can specialize in a scripted class. Tables 5-1 and 4-2 describe how pipe is implemented by certain classes in the core classes.

Table 5-1: Pipe forms

Piping From

Results in

instance of Collection

each item in the collection is piped to the class or function on the to side of the pipe operator.

instance of Range

each number in the range is piped to the class or function on the to side of the pipe operator.

Table 5-2: Pipe forms

Piping To

Results in

Collection class

a new instance of that class is created and the source items are collected into that instance.

instance of Collection

source items are appended to the end of the existing Collection object.

a function

source items are used as the arguments to the function and an instance of LinkedList is used to collect the results. The function being piped to must be defined to be a function with only one argument.

The following examples demonstrate the use of pipes:

1 to 5 | LinkedList
#(1, 2, 3, 4, 5) as LinkedList

#(3,4,5,6) | #(1,2)
#(1, 2, 3, 4, 5, 6)

function double n -> 2 * n
1 to 10 | double
#(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

#(1, 2, 3, 4) | (n -> 2 * n)
#(2, 4, 6, 8)

You can also "cascade" pipe expressions, that is, string two or more pipe expressions together.



function squares n -> n * n
1 to 5 | double | squares
#(4, 16, 36, 64, 100)
A useful trick is to pipe a long collection to the print function. The collection would otherwise be displayed in truncated form. The following example collects a list of generic functions that the Rect class implements. Only the first few values are reproduced here.

getAllGenerics Rect | print
init()
localEqual()
prin()
morph()
copy()
. . .

As an intermediate step, the collection could be piped to SortedArray, so that it is alphabetized.

getAllGenerics Rect | SortedArray | print
bboxGetter()
copy()
drawSelf()
finalize()
heightGetter()
heightSetter()
. . .

This script creates a list of classes of objects that implement the generic function pipe. Note the use of an anonymous function. The anonymous function is especially useful here as a "package" around canObjectDo, a generic function that requires two arguments. Since pipe works only with functions requiring one argument, the anonymous function solves the problem. It takes the one argument supplied by pipe, passes it to canObjectDo, and supplies pipe as the second argument to canObjectDo. The result of the anonymous function is then passed on to the next pipe.

getSubs RootObject | (y -> if canObjectDo y pipe then y else empty) \
		| SortedArray | print
AccessoryContainer
ActionList
ActuatorController
ApplyTree
Array
ArrayList
Btree
ByteString
. . .


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.